package cn.yunhe.test;

import cn.yunhe.dao.StudentMapper;
import cn.yunhe.entity.Block;
import cn.yunhe.entity.Course;
import cn.yunhe.entity.News;
import cn.yunhe.entity.Student;
import cn.yunhe.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.List;

public class Demo1 {

    @Test
    public void test1() {
        SqlSession session = SqlSessionFactoryUtil.getSession();
        List<Block> list = session.selectList("Block.selectAllInfo");

        for (Block block : list) {
            System.out.println("版块：" + block.getBlockName());
            for (News n : block.getNews()) {
                System.out.println("新闻：" + n.getTitle());
            }
        }
    }

    @Test
    public void test2() {
        SqlSession session = SqlSessionFactoryUtil.getSession();
        List<News> newsList = session.selectList("News.selectBlockByNews");

        for (News n : newsList) {
            System.out.println("新闻：" + n.getTitle() + "所属版块是：" + n.getBlock().getBlockName());

        }
    }

    @Test
    public void manyTomany() throws Exception {
        SqlSession session = SqlSessionFactoryUtil.getSession();
        StudentMapper mapper = session.getMapper(StudentMapper.class);

        List<Student> students = mapper.selectStudentCourse();
        session.commit();
        for (Student stu : students) {
            System.out.println(stu.getId() + "," + stu.getName() + "," + stu.getSex() + "," + stu.getAge() + ":");
            List<Course> courses = stu.getCourses();
            for (Course cou : courses) {
                System.out.println(cou.getId() + "," + cou.getName() + "," + cou.getCredit());
            }
        }

    }


}
